package com.bittech.chatroom.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Author: yuisama
 * @Date: 2019-06-18 15:13
 * @Description:公共工具类
 */
public class CommUtils {
    private static final Gson GSON = new GsonBuilder().create();

    public static boolean strIsEmpty(String msg) {
        if (msg == null || msg.equals(""))
            return true;
        return false;
    }

    /**
     * 加载配置文件
     * @param fileName
     * @return
     */
    public static Properties loadProperties(String fileName) {
        Properties properties = new Properties();
        InputStream in = CommUtils.class.getClassLoader()
                .getResourceAsStream(fileName);
        try {
            properties.load(in);
        } catch (IOException e) {
            System.err.println("加载配置文件失败，请检查文件路径");
        }
        return properties;
    }

    /**
     * 对象序列化为JSON
     * @param obj
     * @return
     */
    public static String object2Json(Object obj) {
        return GSON.toJson(obj);
    }

    /**
     * JSON字符串反序列为实体
     * @param jsonStr
     * @param objClass
     * @return
     */
    public static Object json2Object(String jsonStr,Class objClass) {
        return GSON.fromJson(jsonStr,objClass);
    }
}
